package hhm.android.my

import android.content.Intent
import android.os.Bundle
import android.os.Handler
import android.view.View
import androidx.activity.result.ActivityResultLauncher
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.trello.rxlifecycle4.RxLifecycle
import com.trello.rxlifecycle4.android.ActivityEvent
import hhm.android.library.bus.Bus
import hhm.android.library.utils.OnMultiClickListener
import hhm.android.library.utils.SpacesItemDecoration
import hhm.android.my.databinding.ActivityMyBabyListBinding
import siau.android.base.*
import siau.android.http.model.ChildIdModel
import siau.android.http.model.ChildModel
import siau.android.library.KeyString

@Route(path = BABY_LIST_ROUTE)
class MyBabyListActivity : SBBaseActivity() {

    lateinit var dataBinding: ActivityMyBabyListBinding
    var list = ArrayList<ChildModel>()
    lateinit var adapter: MyBabyListAdapter
    lateinit var start: ActivityResultLauncher<Intent>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_my_baby_list)
        initTitle(getString(R.string.my_baby))
        dataBinding.lifecycleOwner = this

        start = registerForActivityResult(IntentIntActivityResultContact()) {
            if (it == 1001) {
                showLoadingFragment(R.id.activity_my_baby_list_fl)
                initData()
            }
        }

        if (SBApplication.userData.isCreator == 1) {
            dataBinding.activityMyBabyListAdd.setOnClickListener(object : OnMultiClickListener() {
                override fun onMultiClick(v: View?) {
                    val i = Intent(this@MyBabyListActivity, BabyProfileActivity::class.java)
                    i.putExtra(KeyString.type, 0)
                    start.launch(i)
                }
            })
        } else {
            dataBinding.activityMyBabyListAdd.visibility = View.GONE
        }

        dataBinding.activityMyBabyListRecycler.layoutManager = LinearLayoutManager(this)
        dataBinding.activityMyBabyListRecycler.addItemDecoration(SpacesItemDecoration(dp2px(10f)))
        adapter = MyBabyListAdapter(this, list, object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                val b = v?.tag as ChildModel
                val i = Intent(this@MyBabyListActivity, BabyProfileActivity::class.java)
                i.putExtra(KeyString.type, BabyProfileActivity.TYPE_CHANGE)
                i.putExtra(KeyString.childId, b.childId)
                start.launch(i)
            }
        }, object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                showLoadingFragment(R.id.activity_my_baby_list_fl)
                val b = v?.tag as ChildModel
                val r = ChildIdModel(b.childId)
//                if (SBApplication.startCalculate.value == true && SBApplication.calculateChild == b.childId) {
//                    SBApplication.startCalculate.value = false
//                }
                HttpHelper().deleteChild(r)?.compose(RxLifecycle.bindUntilEvent(lifecycle(), ActivityEvent.STOP))?.subscribe({
                    initData()
                }, {
                    removeLoadingFragment()
                    showToast(it.message)
                })
            }
        })
        dataBinding.activityMyBabyListRecycler.adapter = adapter

        if (SBApplication.userData.children!!.size == 0) {
            showLoadingFragment(R.id.activity_my_baby_list_fl)
            Handler().postDelayed({ showNoDataView() }, 100)
        } else {
            adapter.setData(SBApplication.userData.children!!)
        }

        showLoadingFragment(R.id.activity_my_baby_list_fl)
        initData()
    }

    private fun initData() {
        HttpHelper().myBabyList()?.subscribe({
            if (it.size == 0) {
                showNoDataView()
                return@subscribe
            }
            removeLoadingFragment()

            this.list = it
            SBApplication.userData.children = it
            var has = false
            list.forEach { i ->
                if (i.childId == SBApplication.communityChildId && i.childId != -1) {
                    i.select = true
                    SBApplication.communityChildId = i.childId
                    has = true
                }
            }
            if (!has && list.size > 0) {
                list[0].select = true
                SBApplication.communityChildId = list[0].childId
            }

            adapter.setData(list)

        }, {
            removeLoadingFragment()
            showToast(it.message)
        })
    }

    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        val type = intent?.getStringExtra(KeyString.type)
        if (KeyString.becomeCreator == type) {
            dataBinding.activityMyBabyListAdd.visibility = View.VISIBLE
            dataBinding.activityMyBabyListAdd.setOnClickListener(object : OnMultiClickListener() {
                override fun onMultiClick(v: View?) {
                    val i = Intent(this@MyBabyListActivity, BabyProfileActivity::class.java)
                    i.putExtra(KeyString.type, 0)
                    start.launch(i)
                }
            })
        }
    }
}